package com.wwwarehouse.financialcenter.fragment.moneywarehouse.homepage;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.SpaceItemDecoration;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.adapter.moneyhouse.MoneyHouseAdapter;
import com.wwwarehouse.financialcenter.bean.bindingcard.MoneyHouseInfomationBean;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Joseph on 2017/11/14.
 *         <p/>
 *         选择钱仓
 */

public class ChooseMoneyHouseFragment extends TheParentFragment implements MoneyHouseAdapter.OnItemClickListener {

    private RecyclerView mRvContent = null;
    private MoneyHouseAdapter mAdapter;
    private int mHeight;
    private List<MoneyHouseInfomationBean.DataBean> data;
    private CardDeskMessageStableCardBean.TasksBean tasksBean;

    @Override
    protected int getContentId() {
        return R.layout.financial_center_fragment_recycler_pagers;
    }

    @Override
    protected void init(View view) {
        mRvContent = (RecyclerView) view.findViewById(R.id.rv_content);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle bundle = getArguments();
        final List<MoneyHouseInfomationBean.DataBean> mList = new ArrayList<>();
        if (bundle != null) {
            int pos = bundle.getInt("pos") - 1;
            int num = bundle.getInt("num");
            data = (List<MoneyHouseInfomationBean.DataBean>) bundle.getSerializable("moneyHomeData");
            tasksBean = bundle.getParcelable("tasksBean");
            for (int i = pos * num; i < num + pos * num; i++) {
                if (i >= 0 && i < data.size()) {
                    mList.add(data.get(i));
                }
            }
        }
        mRvContent.addItemDecoration(new SpaceItemDecoration(ConvertUtils.dip2px(mActivity, 5), ConvertUtils.dip2px(mActivity, 5)));
        mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 3));
        final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                observer.removeGlobalOnLayoutListener(this);
                mHeight = mRvContent.getHeight();
                mAdapter = new MoneyHouseAdapter(mActivity, mList, mHeight);
                mRvContent.setAdapter(mAdapter);
                mAdapter.setOnItemClickListener(ChooseMoneyHouseFragment.this);
            }
        });

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void ItemClickListener(int position, View view, MoneyHouseInfomationBean.DataBean data) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("moneyHomeData", data);
        bundle.putSerializable("moneyHomeDataList", (Serializable) this.data);
        bundle.putParcelable("tasksBean", tasksBean);
        MoneyHomePageFragment moneyHomePageFragment = new MoneyHomePageFragment();
        moneyHomePageFragment.setArguments(bundle);
        pushFragment(moneyHomePageFragment);
    }
}
